﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using SCResources.BusinissLayer;
using SCResources.General;


namespace Serie_Counter.Presentatie
{
    public partial class SettingsForm : Form
    {
        private Settings settings;

        public Settings Settings
        {
            get
            {
                return settings;
            }
        }

        public SettingsForm(Settings settings)
        {
            this.settings = settings;
            InitializeComponent();
            
            //ini form values
            textBoxDefaultSave.Text = settings.SerieListSavePath;
            checkBoxAutoStart.Checked = settings.AutoStart;
            checkBoxRemember.Checked = settings.EnableRember;
            checkBoxCloseAppOption.Checked = settings.CloseWithMainForm;
            if(settings.WarningDelay != -1)
            {
                checkBoxWarnDelay.Checked = true;
                textBoxWarnDelay.Text = settings.WarningDelay.ToString();
            }
            labelWarnDelayError.Visible = false;

            //move options
            checkBoxEnableFileMoving.Checked = settings.EnableAutoMove;
            textBoxRootFolder.Text = settings.RootFolder;
            comboBoxCheckInterval.SelectedItem = settings.CheckInterval.ToString();
            switch (settings.SelectedMoveOption)
            {
                case AutoMoveOption.Manual:
                    radioButtonManual.Checked = true;
                    break;
                case AutoMoveOption.Startup:
                    radioButtonCheckAtStartup.Checked = true;
                    break;
                case AutoMoveOption.Time:
                    radioButtonMin.Checked = true;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private void buttonChange_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "xml| *.xml";
            
            if(save.ShowDialog() == DialogResult.OK)
            {
                settings.SerieListSavePath = save.FileName;
                textBoxDefaultSave.Text = settings.SerieListSavePath;
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            //make sure the shortcut is only added or deleted when the setting is changed.
            if(settings.AutoStart != checkBoxAutoStart.Checked)
            {
                Cursor = Cursors.WaitCursor;
                settings.AutoStart = checkBoxAutoStart.Checked;
                //uncomment for BUILD
                if (settings.AutoStart)
                {
                    StartupFolderUlti.startWithWindows();
                }
                if (!settings.AutoStart)
                {
                    StartupFolderUlti.unsetStartWithWindows();
                }
                Cursor = Cursors.Default;
            }
            
            
            settings.EnableRember = checkBoxRemember.Checked;
            settings.CloseWithMainForm = checkBoxCloseAppOption.Checked;

            //automove settings
            if (checkBoxEnableFileMoving.Checked)
            {
                if(Directory.Exists(textBoxRootFolder.Text))
                {
                    settings.RootFolder = textBoxRootFolder.Text;
                    settings.EnableAutoMove = checkBoxEnableFileMoving.Checked;
                    if(radioButtonMin.Checked)
                    {
                        settings.SelectedMoveOption = AutoMoveOption.Time;
                        settings.CheckInterval = int.Parse(comboBoxCheckInterval.SelectedItem.ToString());
                    }
                    else if (radioButtonCheckAtStartup.Checked)
                    {
                        settings.SelectedMoveOption = AutoMoveOption.Startup;
                    }
                    else
                    {
                        settings.SelectedMoveOption = AutoMoveOption.Manual;
                    }
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("Root folder doesn't exsist!", "Root folder Error", MessageBoxButtons.OK,
                                    MessageBoxIcon.Stop);
                }
            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void checkBoxWarnDelay_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBoxWarnDelay.Checked)
            {
                textBoxWarnDelay.Enabled = true;
            }
            else
            {
                settings.WarningDelay = -1;
                buttonSave.Enabled = true;
            }
        }

        private void textBoxWarnDelay_KeyUp(object sender, KeyEventArgs e)
        {
            if (checkBoxWarnDelay.Checked)
            {
                try
                {
                    settings.WarningDelay = int.Parse(textBoxWarnDelay.Text);
                    if (settings.WarningDelay == 0)
                    {
                        throw new ArgumentException("Getal mag niet 0 zijn");
                    }
                    buttonSave.Enabled = true;

                }
                catch (Exception)
                {
                    labelWarnDelayError.Visible = true;
                    buttonSave.Enabled = false;
                }
            }
        }

        private void buttonSelectRootFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();

            if(openFolderDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxRootFolder.Text = openFolderDialog.SelectedPath;
            }
        }

        private void checkBoxEnableFileMoving_CheckedChanged(object sender, EventArgs e)
        {
            buttonSelectRootFolder.Enabled = !buttonSelectRootFolder.Enabled;
            textBoxRootFolder.Enabled = !textBoxRootFolder.Enabled;
        }

        private void radioButtonMin_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxCheckInterval.Enabled = radioButtonMin.Checked;
        }
        
    }
}
